package com.central.apps.controller;

import com.central.app.common.cache.CfgRedisCache;
import com.central.app.service.HandlerService;
import com.central.apps.handler.timeTask.inspcriterion.InspplanTask;
import com.central.apps.util.LoginUserUtil;
import com.central.apps.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Inventory")
@Slf4j
public class InventoryExportController {
    @Autowired
    private HandlerService handlerService;
    @Autowired
    private CfgRedisCache cfgRedisCache;

    @PostMapping("/InventoryExport")
    public void InventoryExport(@RequestBody Map<String,Object> params , HttpServletResponse response) throws IOException {
        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("Sheet1");
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("物料编号");
            row.createCell(1).setCellValue("物料名称");
            row.createCell(2).setCellValue("库房编号");
            row.createCell(3).setCellValue("库房名称");
            row.createCell(4).setCellValue("品牌");
            row.createCell(5).setCellValue("库存余量");
            row.createCell(6).setCellValue("预留余量");
            row.createCell(7).setCellValue("可用余量");
            row.createCell(8).setCellValue("计量单位");

            String itemnum = MapUtils.getString(params, "itemnum");
            String itemdesc = MapUtils.getString(params, "itemdesc");
            String storenum = MapUtils.getString(params, "storenum");
            String storedesc = MapUtils.getString(params, "storedesc");
            String spemodel = MapUtils.getString(params, "spemodel");
            String orderunit = MapUtils.getString(params, "orderunit");
            String sql = "select t.itemnum,t.itemdesc,t.storenum,t.storedesc,t.spemodel,t.curbaltotal,t.reservedqty,t.avblbalance,t.orderunit from gq_inventory as t where 1=1";
            StringBuilder str = new StringBuilder();
            str.append(sql);
            if (StringUtils.isNotBlank(itemnum)) {
                str.append(" and itemnum like '%" + itemnum + "%'");
            }
            if (StringUtils.isNotBlank(itemdesc)) {
                str.append(" and itemdesc like '%" + itemdesc + "%'");
            }
            if (StringUtils.isNotBlank(storenum)) {
                str.append(" and storenum like '%" + storenum + "%'");
            }
            if (StringUtils.isNotBlank(storedesc)) {
                str.append(" and storedesc like '%" + storedesc + "%'");
            }
            if (StringUtils.isNotBlank(spemodel)) {
                str.append(" and spemodel like '%" + spemodel + "%'");
            }
            if (StringUtils.isNotBlank(orderunit)) {
                str.append(" and orderunit like '%" + orderunit + "%'");
            }


            //添加权限
            String deptList = LoginUserUtil.getDeptList("inventoryApp", "gq_inventory",cfgRedisCache);
            if(StringUtils.isNotBlank(deptList)){
                str.append(" "+deptList);
            }
            List<Map<String, Object>> bySql = handlerService.findBySql(str.toString());
            if (CollectionUtils.isNotEmpty(bySql)) {
                int rownum = 1;
                for (Map<String, Object> map : bySql) {
                    HSSFRow row1 = sheet.createRow(rownum);
                    row1.createCell(0).setCellValue(MapUtils.getString(map, "itemnum"));
                    row1.createCell(1).setCellValue(MapUtils.getString(map, "itemdesc"));
                    row1.createCell(2).setCellValue(MapUtils.getString(map, "storenum"));
                    row1.createCell(3).setCellValue(MapUtils.getString(map, "storedesc"));
                    row1.createCell(4).setCellValue(MapUtils.getString(map, "spemodel"));
                    row1.createCell(5).setCellValue(MapUtils.getString(map, "curbaltotal"));
                    row1.createCell(6).setCellValue(MapUtils.getString(map, "reservedqty"));
                    row1.createCell(7).setCellValue(MapUtils.getString(map, "avblbalance"));
                    row1.createCell(8).setCellValue(MapUtils.getString(map, "orderunit"));
                    rownum++;
                }
            }

            //输出Excel文件
            OutputStream output = response.getOutputStream();
            response.reset();
            String exportFileName = "库存台账.xls";
            String fileNameURL = URLEncoder.encode(exportFileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileNameURL + ";" + "filename*=utf-8''" + fileNameURL);
            //response.setContentType("application/x-xls");
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            wb.write(output);
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
